package com.huang.PC.pcDemo;


import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

class zy{
    private BlockingQueue<String> queue;
    private  volatile  boolean flag=true;

    public zy(BlockingQueue<String> queue) {
        this.queue = queue;
    }

    public void con() throws Exception {
        while (flag) {
            queue.take();
            System.out.println("消费了。。。");
        }

    }


    public void pro() throws Exception {
while (flag) {
    queue.put("a");
    System.out.println("生产了。。。");
}
    }
    public void stop(){
        flag=false;
    }
}

public class queue {
    public static void main(String[] args) throws InterruptedException {
        zy zy = new zy(new ArrayBlockingQueue(3));
        for (int i = 0; i < 3; i++) {
            new Thread(()->{
                try {
                    zy.con();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }

        for (int i = 0; i < 3; i++) {
            new Thread(()->{
                try {
                    zy.pro();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }

        TimeUnit.MILLISECONDS.sleep(20);

        zy.stop();
    }

}
